<?php
require_once(APPPATH."models/base.php");

class guest_book_model extends base_model
{	
	function get_wrap_content()
	{
		return $this->content;
	} 
	
	function get_text()
	{
		return strip_tags($this->content);
	}
	
	function get_profile($length)
	{
		$text = str_replace( '&nbsp;', '', $this->text);  
		$text = str_replace( "\r\n", '', $text);    
		$text = str_replace( '\r', '', $text);      
		$text = str_replace( '\n', '', $text);      
		return mb_substr($text, 0,$length,'UTF8');     
	}
	 
	function fetch_images($model = Fetch_Image_Model::OnlySrc)
	{ 
		if($model == Fetch_Image_Model::OnlySrc)
		{
			$pattern = "/<img .*?src=\"(.+?)\".*?>/";
			preg_match_all($pattern, $this->content, $m);
			$images = array();
			if(isset($m[1]))
			{
				foreach ($m[1] as $image)
				{
					$images[] = $image;
				}
			}
			return $images;
		}
		else
		{
			$pattern = "/<img .*?src=(.+?).*?>/";
			preg_match_all($pattern, $this->content, $m);
			// print_r($m);
			$ob_images = Array();
			if(isset($m[0]))
			{
				foreach($m[0] as $img_html)
				{
					$ob_image = new stdClass();
					$p = "/src=\"(.+?)\"/";
					preg_match($p, $img_html,$img);
					if(isset($img[1]))
					{
						$ob_image->src = $img[1];
						$p = "/title=\"(.+?)\"/";
						preg_match($p, $img_html,$title);
						$ob_image->title = isset($title[1])?$title[1]:'';
						$p = "/alt=\"(.+?)\"/";
						preg_match($p, $img_html,$alt);
						$ob_image->alt = isset($alt[1])?$alt[1]:'';
					}
					$ob_images[] = $ob_image;
				}
			}
			return $ob_images ;  
		} 
	}
   	
	function fetch_image($index,$model = Fetch_Image_Model::OnlySrc)
	{
		$images = $this->fetch_images();
		if(isset($images[$index]))
		{
			return $images[$index];
		}
		return null ; 
	}
}

class guest_book_manager extends base_manager
{
	public function get_list($where = Array())
	{
		$query_result = new stdClass();
		$page = array_remove($where,'page');$page == null && $page = 1 ;
		$page_size = array_remove($where,'page_size');
		$order_by = array_remove($where,'order_by');
		
		$t_guest_book = _T("guest_book");$t_guest_book_class = _T("guest_book_class");
		
		
		$where_string = "where 1=1 ";
		
		$where_string .= $this->build_where_string($where);
		
		$sql_pattern =
		@"select %s from $t_guest_book guest_book %s";
		
		$count_sql = sprintf($sql_pattern,"count(*) as row_number",$where_string);
		$query_result->record_count = $this->db->query($count_sql)->row()->row_number;
		
		if($order_by != null)
		{
			$sql_pattern .= " order by $order_by";
		}
		else
		{
			$sql_pattern .= " order by id desc";
		}
		// var_dump($page_size);exit;
		if($page_size != null)
		{
			$begin_index = ($page-1)*$page_size;
			$query_result->page_count = intval($query_result->record_count/$page_size)-1;
			$sql_pattern .= " limit $begin_index,$page_size";
		}
		
		$result_sql = sprintf(
			$sql_pattern,
			@"guest_book.*",
			$where_string
		);
		
		$this->query_result = $query_result;
		return $this->convert_list(
				$this->db->query($result_sql)->result()
		);
	}
}

?>